package com.gdou.crrp.common;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SmsAPI {
    //accessKeyId和accessKeySecret
    private static final String accessKeyId = "LTAI4FvUa6vcCHZYnNP5wwVw";
    private static final String accessKeySecret = "mmXoWaF3JZjwW4ovJe4hVc4DxFyjt0";

    //短信发送方法
    public static CommonResponse sendSms(Map<String,String> paramMap){

        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        //组装请求对象
        CommonRequest request = new CommonRequest();
        //提交方法
        request.setMethod(MethodType.POST);
        //服务域名
        request.setDomain("dysmsapi.aliyuncs.com");
        //版本号
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //发送手机号
        request.putQueryParameter("PhoneNumbers",paramMap.get("phone"));
        //签名名称
        request.putQueryParameter("SignName", "城市预约挂号平台");
        //模板CODE
        request.putQueryParameter("TemplateCode", "SMS_178765959");
        //短信模板变量对应的实际值
        request.putQueryParameter("TemplateParam","{\"code\":\"" + paramMap.get("code") + "\"}");

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        }catch (ServerException e) {
            e.printStackTrace();
        }catch (ClientException e){
            e.printStackTrace();
        }
        return response;
    }

}
